from wxauto import WeChat
import time
import requests

wx = WeChat()

def get_intelligent_reply(message):
    # 使用DeepSeek的API接口获取智能回复
    api_url = "https://api.deepseek.com/v1/chat/completions"
    headers = {
        "Authorization": "Bearer sk-a5729cf24ebb4d7490abc786987304d5",
        "Content-Type": "application/json"
    }
    data = {
        "model": "deepseek-chat",
        "messages": [
            {"role": "user", "content": message}
        ],
        "temperature": 0.5
    }
    try:
        response = requests.post(api_url, headers=headers, json=data)
        if response.status_code == 200:
            response_data = response.json()
            # 解析返回的JSON数据获取回复内容
            reply = response_data['choices'][0]['message']['content']
            return reply
        else:
            return f"抱歉，接口请求失败，状态码：{response.status_code}"
    except Exception as e:
        print(f"调用接口失败：{e}")
        return "抱歉，服务暂时不可用，请稍后再试"

def auto_reply_multiple():
    initial_msgs = {}
    print("自动回复机器人已启动，按 Ctrl+C 退出。")
    while True:
        try:
            messages = wx.GetAllMessage
            if messages:
                for msg in messages:
                    print(msg)
                    friend = msg.get("sender", "")
                    content = msg.get("msg", "")
                    if initial_msgs.get(friend) != content:
                        print(f"来自 [{friend}] 的消息：{content}")
                        # 获取智能回复
                        reply = get_intelligent_reply(content)
                        wx.ChatWith(friend)
                        wx.SendMsg(reply)
                        print(f"已回复 [{friend}]：{reply}")
                        initial_msgs[friend] = content
            time.sleep(1)
        except KeyboardInterrupt:
            print("机器人已停止运行。")
            break
        except Exception as e:
            print(f"发生错误：{e}")
            time.sleep(1)

auto_reply_multiple()